// pages/goods_list/index.js
import { request } from "../../request/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Tabs: [
      {
        id: 0,
        name: '综合',
        isactive: true
      },
      {
        id: 1,
        name: '销量',
        isactive: false
      },
      {
        id: 2,
        name: '评价',
        isactive: false
      }
    ],
    goodList: []
  },
  Params: {
    query: '',
    cid: '',
    pagenum: 1,
    pagesize: 10
  },
  totelpage: 0,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.Params.cid = options.cid

    this.getgoodlist()

  },
  onReachBottom() {
    if (this.Params.pagenum >= this.totelpage) {
      wx.showToast({
        title: '页面已经到低啦',
        icon: 'none',

      });


    } else {
      this.Params.pagenum++
      this.getgoodlist()
    }
  },
  async getgoodlist() {
    const res = await request({ url: "https://api-hmugo-web.itheima.net/api/public/v1/goods/search", data: this.Params })

    this.totelpage = Math.ceil(res.data.message.total / this.Params.pagesize)


    const goodList = res.data.message.goods

    this.setData({ goodList: [...this.data.goodList, ...goodList] })
    wx.stopPullDownRefresh()

  },
  handelactiveTabsClick(e) {


    const { index } = e.detail
    let { Tabs } = this.data
    Tabs.forEach((element, i) => {
      i === index ? element.isactive = true : element.isactive = false
    });
    this.setData({
      Tabs
    })

  },
  onPullDownRefresh() {
    this.Params.pagenum = 1
    this.setData({
      goodList: []

    })
    this.getgoodlist()
  }


})